<?php

namespace services\base;

use common\models\base\Drug;
use common\models\base\DrugConfig;
use common\components\Service;

/**
 * Class QueueService
 * @package services\base
 */
class DrugService extends Service
{
    /**
     * 队列药品名称
     * @param int $drugId
     * @param string|null $drugName
     * @param string|null $drugUnit
     * @param int|null $drugType
     * @param float|null $drugVolume
     * @param int $waterType
     * @param int $waterVolume
     * @return string
     */
    public static function getDrugContent(int $drugId, ?string $drugName, ?string $drugUnit, ?int $drugType, ?float $drugVolume, int $waterType, int $waterVolume): string
    {
        $drugContent = '';
        if ($drugId) {
            $drugVolume = round($drugVolume, 2);
            $drugContent .= "{$drugName}{$drugVolume}{$drugUnit}";
        }
        if ($drugType == DrugConfig::DRUG_TYPE_JD && $waterType) {
            $waterUnit = DrugConfig::WATER_UNIT;
            $waterTypeStr = DrugConfig::$waterTypes[$waterType] ?? '';
            if ($drugContent) {
                $drugContent .= " + ";
            }
            $drugContent .= "{$waterTypeStr}{$waterVolume}{$waterUnit}";
        }
        return $drugContent;
    }

    /**
     * 打印药品名称
     * @param int $drugId
     * @param string|null $drugName
     * @param string|null $drugUnit
     * @param int|null $drugType
     * @param int|null $drugVolume
     * @param int $waterType
     * @param int $waterVolume
     * @return string
     */
    public static function getDrugPrintContent(int $drugId, ?string $drugName, ?string $drugUnit, ?int $drugType, ?int $drugVolume, int $waterType, int $waterVolume): string
    {
        $drugContent = '';
        if ($drugId) {
//            $drugVolume = round($drugVolume, 2);
            $drugContent .= "{$drugName}{$drugVolume}{$drugUnit}";
        }
        if ($drugType == DrugConfig::DRUG_TYPE_JD && $waterType) {
            $waterUnit = DrugConfig::WATER_UNIT;
            $waterTypeStr = DrugConfig::$waterPrintTypes[$waterType] ?? '';
            if ($drugContent) {
                $drugContent .= " + ";
            }
            $drugContent .= "{$waterTypeStr}{$waterVolume}{$waterUnit}";
        }
        return $drugContent;
    }

    /**
     * @Notes: 获取输液药品列表
     * @Interface getListPage
     * @param array $params 查询条件
     * @param int $page 页码
     * @param int $pageSize 每页数据量
     * @return array
     * @author: chenyuebin
     * @Time: 2022/2/14 0014   14:13
     */
    public function getListPage(array $params = [], int $page = 0, int $pageSize = 0)
    {
        $query = Drug::find()
            ->select(['id', 'drug_name', 'drug_type', 'drug_unit', 'status', 'sort'])
            ->where(['status' => [Drug::STATUS_ENABLE, Drug::STATUS_INVALID]]);
        // 总数
        $total = $query->count();
        // 分页
        if ($page && $pageSize) {
            $query->limit($pageSize)->offset(($page - 1) * $pageSize);
        }

        $list = $query
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->asArray()
            ->all();
        // 格式化
        $data = [];
        foreach ($list as $row) {

            $data[] = [
                'id' => $row['id'],
                'drug_name' => $row['drug_name'],
                'sort' => $row['sort'],
                'drug_unit' => $row['drug_unit'],
                'drug_type' => Drug::$drugTypes[$row['drug_type']],
                'drug_type_id' => $row['drug_type'],
                'status' => $row['status'],
                'status_str' => Drug::$statuss[$row['status']],
            ];
        }

        return [
            'page' => (int)$page,
            'pageSize' => (int)$pageSize,
            'total' => (int)$total,
            'list' => $data,
        ];
    }

    /**
     * @Notes:新增、编辑输液药品
     * @Interface drugSet
     * @param array $params
     * @param int $id
     * @return bool
     * @throws \Exception
     * @author: chenyuebin
     * @Time: 2022/2/14 0014   14:24
     */
    public function drugSet(array $params, int $id = 0)
    {
        if ($id) {
            $drug = Drug::findOne(['id' => $id]);
        } else {
            $drug = new Drug();
        }

        $drug->attributes = $params;
        return $drug->saveAndCheckResult();
    }

    /**
     * @Notes:获取输液药品信息
     * @Interface drugInfo
     * @param $condition
     * @return array|\yii\db\ActiveRecord|null
     * @author: chenyuebin
     * @Time: 2022/2/14 0014   14:23
     */
    public function drugInfo($condition)
    {
        return Drug::find()->where($condition)->asArray()->one();
    }

    /**
     * @Notes:删除药品
     * @Interface Drugdel
     * @param int $id
     * @return int
     * @author: chenyuebin
     * @Time: 2022/2/14 0014   14:28
     */
    public function Drugdel(int $id)
    {
        return Drug::updateAll(['status' => Drug::STATUS_DELETE], ['id' => $id]);
    }

    /**
     * 药品列表
     * @return array
     */
    public static function getDrugAllList(): array
    {
        $res = Drug::find()->alias('d')
            ->select([
                'd.id', 'd.drug_name', 'd.drug_unit',
            ])
            ->where([
                'd.status' => [Drug::STATUS_ENABLE, Drug::STATUS_INVALID]
            ])
            ->orderBy(['d.sort' => SORT_ASC, 'd.id' => SORT_DESC])
            ->asArray()
            ->all();
        $data = [];
        foreach ($res as $row) {
            $data[] = [
                'id' => $row['id'],
                'name' => $row['drug_name'],
                'type' => 2,// 固定死，前端区分用【1治疗项目、2输液药品】
            ];
        }
        return [
            'id' => 0,
            'name' => '输液药品',
            'child' => $data,
        ];
    }
}